# Contributor: Leonardo Arena <rnalrd@alpinelinux.org>
# Contributor: August Klein <amatcoder@gmail.com>
# Contributor: Orson Teodoro <orsonteodoro@hotmail.com>
# Contributor: Max Rees <maxcrees@me.com>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=keepassxc
pkgver=2.7.6
pkgrel=2
pkgdesc="Community-driven port of the Windows application Keepass Password Safe"
url="https://keepassxc.org/"
arch="all"
license="GPL-2.0-only OR GPL-3.0-only"
provides="dbus:org.freedesktop.Secrets"
provider_priority=5 # Smallest priority, unusual setup
depends="hicolor-icon-theme"
makedepends="
	argon2-dev
	asciidoctor
	botan3-dev
	cmake
	libqrencode-dev
	libusb-dev
	libxi-dev
	libxtst-dev
	minizip-dev
	pcsc-lite-dev
	qt5-qtbase-dev
	qt5-qtsvg-dev
	qt5-qttools-dev
	qt5-qtx11extras-dev
	readline-dev
	samurai
	zlib-dev
	"
checkdepends="dbus xvfb-run"
install="$pkgname.post-upgrade"
subpackages="$pkgname-doc $pkgname-lang::noarch $pkgname-autotype"
source="https://github.com/keepassxreboot/keepassxc/releases/download/$pkgver/keepassxc-$pkgver-src.tar.xz
	doc-path.patch
	"

build() {
	export CXXFLAGS="$CXXFLAGS -flto=auto"

	cmake -G Ninja -B build \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=/usr/lib \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DKEEPASSXC_BUILD_TYPE=Release \
		-DWITH_XC_AUTOTYPE=ON \
		-DWITH_XC_NETWORKING=ON \
		-DWITH_XC_BROWSER=ON \
		-DWITH_XC_YUBIKEY=ON \
		-DWITH_XC_SSHAGENT=ON \
		-DWITH_XC_KEESHARE=ON \
		-DWITH_XC_UPDATECHECK=OFF \
		-DWITH_XC_FDOSECRETS=ON \
		-DWITH_GUI_TESTS=ON
	cmake --build build
}

check() {
	cd build
	CTEST_OUTPUT_ON_FAILURE=TRUE xvfb-run -a ctest -E 'testcli' -j1 # v2.7.4 testcli fails
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	install -D -t "$pkgdir"/usr/share/doc/$pkgname/ \
		CHANGELOG.md README.md

	cd "$pkgdir"
	mv usr/share/keepassxc/docs/* usr/share/doc/$pkgname/
	rmdir usr/share/keepassxc/docs
}

lang() {
	pkgdesc="Languages for package $pkgname"
	install_if="$pkgname=$pkgver-r$pkgrel lang"
	local langdir='usr/share/keepassxc/translations'

	amove $langdir

	mkdir -p "$pkgdir"/$langdir
	mv "$subpkgdir"/$langdir/keepassxc_en_US.qm "$pkgdir"/$langdir/
}

autotype() {
	pkgdesc="KeePassXC Auto-Type feature for X11"
	install_if="$pkgname=$pkgver-r$pkgrel xorg-server"
	depends=""

	amove usr/lib/keepassxc/libkeepassxc-autotype-xcb.so
}

sha512sums="
58bdf731e1ce15e4a9db5aae27b7740372c4b28e2998b9acf2d1be6ee5b4eead37149ddd3de039d1cb70d34f11b09f43969d36ce4241b3fe3af4adf53d8ba812  keepassxc-2.7.6-src.tar.xz
f1d1978db4da56064f6f7eb37f61f82169020afc18de3be4afbe46f6bbb6292cc9551bf07a454a89bc5b09ebe72a153f08bff9f62492e3c1b69b6919da8be94e  doc-path.patch
"
